package com.wolfpire.workflow.spring.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.jbpm.pvm.internal.task.TaskImpl;
import org.springframework.stereotype.Service;

import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;
import com.wolfpire.system.common.util.StringUtils;
import com.wolfpire.system.model.User;
import com.wolfpire.workflow.dao.JbpmTaskDao;
import com.wolfpire.workflow.dao.MyTaskDao;
import com.wolfpire.workflow.dao.ProInstanceDao;
import com.wolfpire.workflow.em.TaskType;
import com.wolfpire.workflow.om.JbpmTask;
import com.wolfpire.workflow.om.MyTask;
import com.wolfpire.workflow.om.ProInstance;
import com.wolfpire.workflow.spring.MyTaskService;

@Service("myTaskService")
public class MyTaskServiceImpl extends BaseHibernateService<MyTask, Long> implements MyTaskService {

	@Resource private MyTaskDao myTaskDao;
	
	@Resource private JbpmTaskDao jbpmTaskDao;
	
	@Resource private ProInstanceDao proInstanceDao;

	@Override
	public List<MyTask> list(MyTask myTask) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected IBaseHibernateDao<MyTask, Long> getEntityDao() {
		return this.myTaskDao;
	}

	@Override
	public List<JbpmTask> getJbpmTasks(String instanceId) {
		return this.jbpmTaskDao.getJbpmTasks(instanceId);
	}

	@Override
	public void saveMyTask(TaskImpl task, User user, String operateType,
			String remarks, Long nodeId, String attIds) {
		MyTask myTask = myTaskDao.get(task.getDbid());
		Long instanceId = Long.valueOf(task.getProcessInstance().toString().split("\\.")[1].split("\\]")[0]);
		if(myTask!=null){
			myTask.setEndTime(new Date());
			myTask.setOperateType(operateType);
			if(user!=null){
				myTask.setTransactorId(user.getId());
				myTask.setTransactorName(user.getNickName());
			}
			if(!StringUtils.isBlank(remarks)){
				myTask.setRemarks(remarks);
			}
			
			if (!StringUtils.isBlank(attIds)) {
				myTask.setAttIds(attIds);
			}
			//this.myTaskDao.saveOrUpdate(myTask);
		}else{
			myTask = new MyTask();
			myTask.setTaskId(task.getDbid());
			myTask.setTaskName(task.getActivityName());
			myTask.setInstanceId(Long.valueOf(task.getProcessInstance().toString().split("\\.")[1].split("\\]")[0]));
			myTask.setTaskType(TaskType.GENERAL.getIndex());
			myTask.setStartTime(task.getCreateTime());
			myTask.setEndTime(new Date());
			myTask.setOperateType(operateType);
			myTask.setNodeId(nodeId);
			//myTask.setStatus();
			if(user!=null){
				myTask.setTransactorId(user.getId());
				myTask.setTransactorName(user.getNickName());
			}
			if(!StringUtils.isBlank(remarks)){
				myTask.setRemarks(remarks);
			}
			myTask.setAttIds(attIds);
		}
		MyTask lastTask = this.myTaskDao.queryLastTaskInfo(instanceId);
		if (null != lastTask) {
			myTask.setPreTaskId(lastTask.getTaskId());
			myTask.setPreTaskName(lastTask.getTaskName());
			myTask.setPreUserId(lastTask.getTransactorId());
			myTask.setPreUserName(lastTask.getTransactorName());
		}
		
		this.myTaskDao.saveOrUpdate(myTask);
	}

	@Override
	public void saveMyTask(TaskImpl task, User user, String operateType,
			String remarks, Long nodeId, String attIds, String preTaskId,
			String preTaskName, String preUserId, String preUserName) {
		MyTask myTask = myTaskDao.get(task.getDbid());
		Long instanceId = Long.valueOf(task.getProcessInstance().toString().split("\\.")[1].split("\\]")[0]);
		if(myTask!=null){
			myTask.setInstanceId(instanceId);
			myTask.setEndTime(new Date());
			myTask.setOperateType(operateType);
			if(user!=null){
				myTask.setTransactorId(user.getId());
				myTask.setTransactorName(user.getNickName());
			}
			if(!StringUtils.isBlank(remarks)){
				myTask.setRemarks(remarks);
			}
			
			if (!StringUtils.isBlank(attIds)) {
				myTask.setAttIds(attIds);
			}
			//this.myTaskDao.saveOrUpdate(myTask);
		}else{
			myTask = new MyTask();
			myTask.setTaskId(task.getDbid());
			myTask.setTaskName(task.getActivityName());
			myTask.setInstanceId(instanceId);
			myTask.setTaskType(TaskType.GENERAL.getIndex());
			myTask.setStartTime(task.getCreateTime());
			myTask.setEndTime(new Date());
			myTask.setOperateType(operateType);
			myTask.setNodeId(nodeId);
			//myTask.setStatus();
			if(user!=null){
				myTask.setTransactorId(user.getId());
				myTask.setTransactorName(user.getNickName());
			}
			if(!StringUtils.isBlank(remarks)){
				myTask.setRemarks(remarks);
			}
			myTask.setAttIds(attIds);
		}
		if (StringUtils.isNotBlank(preTaskId)) {
			myTask.setPreTaskId(Long.parseLong(preTaskId));
		}
		if (StringUtils.isNotBlank(preTaskName)) {
			myTask.setPreTaskName(preTaskName);
		}
		if (StringUtils.isNotBlank(preUserId)) {
			myTask.setPreUserId(Long.parseLong(preUserId));
		}
		if (StringUtils.isNotBlank(preUserName)) {
			myTask.setPreUserName(preUserName);
		}
		this.myTaskDao.saveOrUpdate(myTask);
	}

	@Override
	public Page<MyTask> queryTaskDetailList(Page<MyTask> page, Long id,
			Long formTypeId) {
		ProInstance instance = this.proInstanceDao.getProByFormId(id, formTypeId);
		if (null != instance) {
			return this.myTaskDao.findMyTasks(page, instance.getInstanceId());
		}
		return new Page<MyTask>();
	}

	@Override
	public MyTask queryLastTaskInfo(Long instanceId) {
		return this.myTaskDao.queryLastTaskInfo(instanceId);
	}

	@Override
	public MyTask findFirstTaskInfo(Long instanceId) {
		return this.myTaskDao.findFirstTaskInfo(instanceId);
	}
	
	 
}
